#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QToolButton>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(ui->actionFolder,&QAction::triggered,this,&MainWindow::viaFolderPath);
    connect(ui->btnFolder,&QToolButton::clicked,this,&MainWindow::viaFolderPath);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::viaFolderPath(){
    int rowPos=0;
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->setHorizontalHeaderLabels(
            QStringList()<<"location"<<"full name"<<"pure name"<<"suffix"
                );
    ui->tableWidget->setRowCount(10);

    //第1个文件夹
    viaFolderPathUser(rowPos);
    //第2个文件夹
    viaFolderPathSys(rowPos);

    //仅用于软件测试
    qDebug("next row: %d",rowPos);

    ui->tableWidget->resizeColumnsToContents();
    ui->tableWidget->resizeRowsToContents();
}

void MainWindow::viaFolderPathSys(int &rowPos){
    //目标文件夹:%ProgramData%\Microsoft\Windows\Start Menu\Programs\Startup
    TCHAR targetPath[MAX_PATH]={0};
    ::GetEnvironmentVariableW(L"ProgramData",targetPath,MAX_PATH);
    wcscat_s(targetPath,L"\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\*");

    //开始进入文件夹
    traversePathUni(targetPath,rowPos);
}

void MainWindow::viaFolderPathUser(int &rowPos){
    //目标文件夹：%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
    TCHAR targetPath[MAX_PATH]={0};
    ::GetEnvironmentVariableW(L"USERPROFILE",targetPath,MAX_PATH);
    wcscat_s(targetPath,L"\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\*");

    //开始进入文件夹
    traversePathUni(targetPath,rowPos);
}

void MainWindow::traversePathUni(const TCHAR* const target,int &rowpos){
    HANDLE hFind;
    WIN32_FIND_DATAW findFileData;

    //寻找目标文件夹下 第1个文件
    hFind=FindFirstFileW(target,&findFileData);
    if(hFind==INVALID_HANDLE_VALUE){
        qDebug()<<"No such path.";
        return;
    }

    int i=1;
    do{
        //仅用于测试
        qDebug("file name: %ls",findFileData.cFileName);

        QString fullname=QString::fromWCharArray(findFileData.cFileName);
        QString suffix=fullname.split(".").last();

        const int pos=fullname.lastIndexOf(".");
        QString pureName=fullname.left(pos);

        QString refPath=QString::fromWCharArray(target);
        const int tmp=refPath.lastIndexOf("\\");
        refPath=refPath.left(tmp);
        //去除refPath末尾\*
        if(fullname!="."
                &&fullname!=".."
                &&fullname!="desktop.ini"
                ){
            ui->tableWidget->setItem(rowpos,0,new QTableWidgetItem(refPath));
            ui->tableWidget->setItem(rowpos,1,new QTableWidgetItem(fullname));
            ui->tableWidget->setItem(rowpos,2,new QTableWidgetItem(pureName));
            ui->tableWidget->setItem(rowpos,3,new QTableWidgetItem(suffix));
            rowpos++;
        }
        //仅用于测试
        //qDebug()<<"full name: "<<fullname<<", pure name: "<<pureName<<", suffix: "<<suffix;
        i++;
    }while (FindNextFileW(hFind,&findFileData));

    FindClose(hFind);
    qDebug()<<"Total file: "<<i-1;
    qDebug()<<"Folder is clear now.";
}
